﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类ProtectedPhoneConfig。
    /// </summary>
    public class ProtectedPhoneConfig
    {
        public ProtectedPhoneConfig()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid ConfigId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ProtectedPhoneConfig where ConfigId=@ConfigId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ConfigId", DbType.Guid, ConfigId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ProtectedPhoneConfig where 1=1 " + strWhere);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.ProtectedPhoneConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ProtectedPhoneConfig(");
            strSql.Append("ConfigId,ConfigType,ConfigMinValue,ConfigMaxValue,ConfigValue,ConfigControlType,CityId,City,CompanyId,Company)");
            strSql.Append(" values (");
            strSql.Append("@ConfigId,@ConfigType,@ConfigMinValue,@ConfigMaxValue,@ConfigValue,@ConfigControlType,@CityId,@City,@CompanyId,@Company)");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            db.AddInParameter(dbCommand, "ConfigId", DbType.Guid, model.ConfigId);
            db.AddInParameter(dbCommand, "ConfigType", DbType.AnsiString, model.ConfigType);
            db.AddInParameter(dbCommand, "ConfigMinValue", DbType.Decimal, model.ConfigMinValue);
            db.AddInParameter(dbCommand, "ConfigMaxValue", DbType.Decimal, model.ConfigMaxValue);
            db.AddInParameter(dbCommand, "ConfigValue", DbType.AnsiString, model.ConfigValue);
            db.AddInParameter(dbCommand, "ConfigControlType", DbType.Int16, model.ConfigControlType);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.ProtectedPhoneConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ProtectedPhoneConfig set ");
            strSql.Append("ConfigType=@ConfigType,");
            strSql.Append("ConfigMinValue=@ConfigMinValue,");
            strSql.Append("ConfigMaxValue=@ConfigMaxValue,");
            strSql.Append("ConfigValue=@ConfigValue,");
            strSql.Append("ConfigControlType=@ConfigControlType,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("City=@City,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("Company=@Company");
            strSql.Append(" where ConfigId=@ConfigId ");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());

            db.AddInParameter(dbCommand, "ConfigId", DbType.Guid, model.ConfigId);
            db.AddInParameter(dbCommand, "ConfigType", DbType.AnsiString, model.ConfigType);
            db.AddInParameter(dbCommand, "ConfigMinValue", DbType.Decimal, model.ConfigMinValue);
            db.AddInParameter(dbCommand, "ConfigMaxValue", DbType.Decimal, model.ConfigMaxValue);
            db.AddInParameter(dbCommand, "ConfigValue", DbType.AnsiString, model.ConfigValue);
            db.AddInParameter(dbCommand, "ConfigControlType", DbType.Int16, model.ConfigControlType);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid ConfigId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProtectedPhoneConfig ");
            strSql.Append(" where ConfigId=@ConfigId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ConfigId", DbType.Guid, ConfigId);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 根据查询条件进行删除
        /// </summary>
        /// <param name="strWhere"></param>
        public static void Delete(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProtectedPhoneConfig ");
            strSql.Append(" where " + strWhere);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /*
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ProtectedPhoneConfig GetModel(Guid ConfigId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ConfigId,ConfigType,ConfigMinValue,ConfigMaxValue from ProtectedPhoneConfig ");
			strSql.Append(" where ConfigId=@ConfigId ");
			SqlParameter[] parameters = {
					new SqlParameter("@ConfigId", SqlDbType.UniqueIdentifier)};
			parameters[0].Value = ConfigId;

			Maticsoft.Model.ProtectedPhoneConfig model=new Maticsoft.Model.ProtectedPhoneConfig();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ConfigId"].ToString()!="")
				{
					model.ConfigId=new Guid(ds.Tables[0].Rows[0]["ConfigId"].ToString());
				}
				model.ConfigType=ds.Tables[0].Rows[0]["ConfigType"].ToString();
				if(ds.Tables[0].Rows[0]["ConfigMinValue"].ToString()!="")
				{
					model.ConfigMinValue=decimal.Parse(ds.Tables[0].Rows[0]["ConfigMinValue"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ConfigMaxValue"].ToString()!="")
				{
					model.ConfigMaxValue=decimal.Parse(ds.Tables[0].Rows[0]["ConfigMaxValue"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}*/

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ConfigId,ConfigType,ConfigMinValue,ConfigMaxValue,ConfigValue,ConfigControlType,CityId,City,CompanyId,Company ");
            strSql.Append(" FROM ProtectedPhoneConfig ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" Order By ConfigType");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ConfigId,ConfigType,ConfigMinValue,ConfigMaxValue,ConfigValue,ConfigControlType,CityId,City,CompanyId,Company ");
            strSql.Append(" FROM ProtectedPhoneConfig ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string StrWhere, string FieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, "ProtectedPhoneConfig");
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, "*");
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.ProtectedPhoneConfig GetModel(Guid ConfigId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ConfigId,ConfigType,ConfigMinValue,ConfigMaxValue,ConfigValue,ConfigControlType,CityId,City,CompanyId,Company from ProtectedPhoneConfig");
            strSql.Append(" where ConfigId=@ConfigId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ConfigId", DbType.Guid, ConfigId);
            Chenxin.Lib.House.Entity.ProtectedPhoneConfig model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.ProtectedPhoneConfig ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.ProtectedPhoneConfig model = new Chenxin.Lib.House.Entity.ProtectedPhoneConfig();
            object ojb;
            ojb = dataReader["ConfigId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConfigId = new Guid(ojb.ToString());
            }

            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.City = dataReader["City"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.Company = dataReader["Company"].ToString();

            ojb = dataReader["ConfigMaxValue"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConfigMaxValue = Convert.ToInt32(ojb);
            }
            ojb = dataReader["ConfigMinValue"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConfigMinValue = Convert.ToInt32(ojb);
            }
            model.ConfigValue = dataReader["ConfigValue"].ToString();
            model.ConfigType = dataReader["ConfigType"].ToString();

            ojb = dataReader["ConfigControlType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConfigControlType = Convert.ToInt32(ojb);
            }
            return model;
        }
        #endregion
    }
}
